import os.path
import sys

students = []

def save():
    with open ("d:/Mycode/python/text.txt","w") as f:
        for s in students:
            f.write(f"{s["姓名"]}\t{s["性别"]}\t{s["班级"]}\t{s["学号"]}\n")
        print(f"存档成功！共存储了{len(students)}条数据！")

def load():
    if not os.path.exists("d:/Mycode/python/text.txt"):
        return

    global students
    students = []
    with open ("d:/Mycode/python/text.txt","r") as f:
        for line in f:
            line = line.strip()#去掉每行的换行符
            tokens = line.split("\t")
            if len(tokens) < 4:
                print(f"文件格式有误！line={line}")
                continue
            student = {
                "姓名": tokens[0],
                "性别": tokens[1],
                "班级": tokens[2],
                "学号": tokens[3]
            }
            students.append(student)
    print(f"读档成功！共读取{len(students)}条数据！")

def menu():
    print("1.<新增同学信息>")
    print("2.<显示所有同学信息>")
    print("3.<根据姓名查找同学信息>")
    print("4.<删除同学信息>")
    print("0.<退出程序>")
    choice = input("请输入：")
    return int(choice)

def insert():
    print("[-------------开始录入-------------]")
    name = input(f"请输入同学姓名：")
    gender = input(f"请输入同学性别：")
    if gender not in ("男", "女"):
        print("*****性别有误！录入失败！*****")
        return
    class_id = input(f"请输入同学班级：")
    student_id = input(f"请输入同学学号：")
    student = {
        "姓名": name,
        "性别": gender,
        "班级": class_id,
        "学号": student_id
    }
    global students
    students.append(student)
    save()
    print("[-------------录入完毕-------------]")

def show():
    print("[----------所有同学信息如下----------]")
    for s in students:
        print(f"{s["姓名"]}\t{s["性别"]}\t{s["班级"]}\t{s["学号"]}")
    print(f"共{len(students)}条数据")
    print("[--------所有同学信息显示完毕---------]")

def find():
    print("[-------------开始查找-------------]")
    count = 0
    name = input("请输入要查找的同学姓名：")
    for s in students:
        if name == s["姓名"]:
            print(f"{s["姓名"]}\t{s["性别"]}\t{s["班级"]}\t{s["学号"]}")
            count += 1
    print(f"共查找到{count}条数据")
    print("[-------------查找完毕-------------]")


def delete():
    print("[-------------开始删除-------------]")
    count = 0
    student_id = input("请输入要删除的同学学号：")
    for s in students:
        if student_id == s["学号"]:
            print(f"{s["姓名"]}同学的信息已删除！")
            students.remove(s)
            count += 1
    print(f"共删除{count}条数据")
    save()
    print("[-------------删除完毕-------------]")

def main():
    print("+------------------------------------------+")
    print("|                                          |")
    print("|             欢迎来到学生信息管理系统          |")
    print("|                                          |")
    print("+------------------------------------------+")
    load()
    while True:
        choice = menu()

        if choice == 0:
            print("Goodbye!")
            sys.exit(0)

        elif choice == 1:
            insert()

        elif choice == 2:
            show()

        elif choice == 3:
            find()

        elif choice == 4:
            delete()

        else:
            print("输入非法！请重新输入：")

main()